﻿using SNMP;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;
using System.Windows.Forms;

namespace SNMP_Agent
{
    public partial class AgentForm : Form
    {
        /// <summary>
        /// 
        /// </summary>
        private Session session;

        /// <summary>
        /// 
        /// </summary>
        private Entity srcEntity;

        /// <summary>
        /// 
        /// </summary>
        private DBOperate dbOperate;

        /// <summary>
        /// 
        /// </summary>
        public AgentForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AgentForm_Load(object sender, EventArgs e)
        {
            SNMPManager.Startup();
            this.session = SNMPManager.CreateSession(this.Handle);
            this.session.ReceiveData += session_ReceiveData;
            this.session.Timeout += session_Timeout;
            this.srcEntity = this.session.CreateEntity(this.GetLocalhostIPAddress(), 1000, 3);

            SNMPManager.StartListen(this.srcEntity);

            this.dbOperate = new DBOperate();
            this.dgvMIB.DataSource = this.dbOperate.SelectAll();

            SNMPManager.Encoding = System.Text.Encoding.GetEncoding("SJIS");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManagerForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            SNMPManager.Cleanup();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void session_ReceiveData(object sender, ReceiveDataEventArgs e)
        {
            switch (e.Pdu.Type)
            {
                case PduData.PduType.Get:
                    break;
                case PduData.PduType.GetNext:
                    break;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void session_Timeout(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private IPAddress GetLocalhostIPAddress()
        {
            // IPGlobalProperties compProperties = IPGlobalProperties.GetIPGlobalProperties();
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in nics)
            {
                if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet && adapter.OperationalStatus == OperationalStatus.Up)
                {
                    foreach (var x in adapter.GetIPProperties().UnicastAddresses)
                    {
                        if (x.Address.AddressFamily.ToString() == "InterNetwork")
                        {
                            return x.Address;
                        }
                    }
                }
            }
            return null;
        }

        private void btnSendTrap_Click(object sender, EventArgs e)
        {
            VariableBindingsList vbl = this.session.CreateVariableBindingsList();
            vbl.Add("1.3.6.1.2.1.1.3.0", new Value(Value.Syntax.Octets, "状態変化時の時刻情報"));
            vbl.Add("1.3.6.1.2.1.1.4.0", new Value(Value.Syntax.Oid, "123.123"));
            PduData pdu = this.session.CreatePdu(PduData.PduType.Trap, 0, 0, 0, vbl);
            Community community = this.session.CreateCommunity("public");
            Entity dstEntity = this.session.CreateEntity("192.168.0.78", 1000, 3);
            this.session.SendMessage(this.srcEntity, dstEntity, community, pdu);
        }
    }
}
